home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Champak 62
/
Volume 62 - JOGO DISK .iso
/
Games
/
penguin_dinner.swf
/
scripts
/
__Packages
/
classes
/
math
/
CatmullRomSpline.as
< prev
next >
Wrap
Text File
|
2008-03-17
|
4KB
|
122 lines
class classes.math.CatmullRomSpline
{
var numVertices = 0;
var APPROX_LINE_LENGTH = 5;
function CatmullRomSpline(xVals, yVals)
{
this.xSamples = xVals;
this.ySamples = yVals;
this.numVertices = this.xSamples.length;
this.tSamples = new Array(this.numVertices);
}
function catmullRom(t, P0, P1, P2, P3)
{
var _loc5_ = t * t;
var _loc7_ = t * _loc5_;
return {x:0.5 * (2 * P1.x + (- P0.x + P2.x) * t + (2 * P0.x - 5 * P1.x + 4 * P2.x - P3.x) * _loc5_ + (- P0.x + 3 * P1.x - 3 * P2.x + P3.x) * _loc7_),y:0.5 * (2 * P1.y + (- P0.y + P2.y) * t + (2 * P0.y - 5 * P1.y + 4 * P2.y - P3.y) * _loc5_ + (- P0.y + 3 * P1.y - 3 * P2.y + P3.y) * _loc7_)};
}
function getAllPoints(approxLineLength)
{
if(approxLineLength == undefined)
{
approxLineLength = this.APPROX_LINE_LENGTH;
}
if(this.verticesPlus == null)
{
this.makeVerticesPlus();
}
if(this.lengths == null)
{
this.getSegmentLengths();
}
var _loc4_ = this.verticesPlus;
var _loc8_ = new Array();
var _loc9_ = 0;
var _loc2_ = undefined;
var _loc16_ = _loc4_.length - 3;
_loc2_ = 0;
while(_loc2_ < _loc16_)
{
var _loc6_ = _loc4_[_loc2_ + 1];
var _loc5_ = _loc4_[_loc2_ + 2];
var _loc7_ = this.catmullRom(0.5,_loc4_[_loc2_],_loc6_,_loc5_,_loc4_[_loc2_ + 3]);
var _loc14_ = _loc7_.x - _loc6_.x;
var _loc12_ = _loc7_.y - _loc6_.y;
var _loc13_ = _loc7_.x - _loc5_.x;
var _loc11_ = _loc7_.y - _loc5_.y;
var _loc15_ = Math.sqrt(_loc14_ * _loc14_ + _loc12_ * _loc12_) + Math.sqrt(_loc13_ * _loc13_ + _loc11_ * _loc11_);
this.lengths[_loc2_] = _loc15_;
var _loc10_ = approxLineLength / _loc15_;
var _loc3_ = 0;
while(_loc3_ < 1)
{
_loc8_[_loc9_] = this.catmullRom(_loc3_,_loc4_[_loc2_],_loc6_,_loc5_,_loc4_[_loc2_ + 3]);
_loc9_;
_loc8_[_loc9_++].vertex = _loc2_;
_loc3_ += _loc10_;
}
_loc2_ = _loc2_ + 1;
}
_loc9_;
_loc8_[_loc9_++] = this.catmullRom(1,_loc4_[_loc2_ - 1],_loc4_[_loc2_],_loc4_[_loc2_ + 1],_loc4_[_loc2_ + 2]);
return _loc8_;
}
function getTOfVertex(index)
{
if(this.lengths == null)
{
this.getSegmentLengths();
}
return this.tSamples[index];
}
function getSegmentLengths()
{
if(this.verticesPlus == null)
{
this.makeVerticesPlus();
}
var _loc7_ = this.verticesPlus;
this.lengths = new Array();
this.totalLength = 0;
var _loc14_ = _loc7_.length - 3;
var _loc2_ = 0;
while(_loc2_ < _loc14_)
{
var _loc6_ = _loc7_[_loc2_ + 1];
var _loc5_ = _loc7_[_loc2_ + 2];
var _loc4_ = this.catmullRom(0.5,_loc7_[_loc2_],_loc6_,_loc5_,_loc7_[_loc2_ + 3]);
var _loc11_ = _loc4_.x - _loc6_.x;
var _loc9_ = _loc4_.y - _loc6_.y;
var _loc10_ = _loc4_.x - _loc5_.x;
var _loc8_ = _loc4_.y - _loc5_.y;
var _loc12_ = Math.sqrt(_loc11_ * _loc11_ + _loc9_ * _loc9_) + Math.sqrt(_loc10_ * _loc10_ + _loc8_ * _loc8_);
this.lengths[_loc2_] = _loc12_;
this.totalLength += _loc12_;
_loc2_ = _loc2_ + 1;
}
this.tSamples[0] = 0;
var _loc13_ = 0;
var _loc15_ = this.lengths.length;
var _loc3_ = 0;
while(_loc3_ < _loc15_)
{
_loc13_ += this.lengths[_loc3_];
this.tSamples[_loc3_ + 1] = _loc13_ / this.totalLength;
_loc3_ = _loc3_ + 1;
}
}
function makeVerticesPlus()
{
this.verticesPlus = new Array();
var _loc5_ = this.xSamples.length;
var _loc2_ = 0;
while(_loc2_ < _loc5_)
{
this.verticesPlus.push({x:this.xSamples[_loc2_],y:this.ySamples[_loc2_]});
_loc2_ = _loc2_ + 1;
}
this.verticesPlus.splice(0,0,this.verticesPlus[0]);
this.verticesPlus.push(this.verticesPlus[this.verticesPlus.length - 1]);
}
}